package com.topscomm.pm;

import javax.annotation.Resource;

import com.topscomm.basic.util.SQLAdapter;
import com.topscomm.mis.MisController;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.tap.common.FlowDatacodeCache;
import com.topscomm.pm.PmSystemConst.SearchConditionType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class PmController<T extends PmEntity, TService extends IPmService<T, ?>>
        extends MisController<T, TService> implements IPmController<T, TService> {
    @Resource(name = "flowDatacodeCache")
    protected FlowDatacodeCache flowDatacodeCache;
	/**
	 * @description:  拼接通用where条件（项目编号、项目名称、项目经理）
	 * @param: paramMap
	 * tableName
	 * @return: java.lang.String
	 * @author Administrator
	 * @date: 2021/2/20 16:08
	 */
	protected String spellingSqlForProject(Map paramMap,String sourceTableName,String sourceTableColumn,String projectColumn) {
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle(" and EXISTS( select id from pmproject existsPmProject where {0}.{1} = existsPmProject.{2}",sourceTableName,sourceTableColumn,projectColumn);
        if (paramMap.containsKey("qry_projectCode")){
        	String value = ConvertUtil.convertToString(paramMap.get("qry_projectCode"));
			sql.appendSingle(" and existsPmProject.code = '{0}'",value);
		}
		if (paramMap.containsKey("qry_projectName")){
			String value = ConvertUtil.convertToString(paramMap.get("qry_projectName"));
			sql.appendSingle(" and existsPmProject.name like '%{0}%'",value);
		}
		if (paramMap.containsKey("qry_projectManager")){
			String value = ConvertUtil.convertToString(paramMap.get("qry_projectManager"));
			sql.appendSingle(" and (existsPmProject.projectmanagername like '%{0}%' or existsPmProject.projectmanagercode = '{0}' )",value);
		}
		sql.appendSingle(")");
        return sql.toString();
    }


	/**
	 * 根据前台返回内容拼接sql语句
	 *
	 * @description
	 * @author:HuDeyong
	 * @date:2021年7月1日上午11:08:28
	 * @modify:
	 */
	public String spellSearchSql(Map<String, Object> paraMap) {
		paraMap = BaseUtil.decodeSecureMap(paraMap);
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		// 取出需要拼接的条件
		for (String key : paraMap.keySet()) {
			String[] strConditions = key.split("_");
			// 只有qryc_字段名_查询类型或qryc_字段名_查询类型_扩展信息 的查询条件才进行拼接
			if (strConditions.length != 3 && strConditions.length != 4) {
				continue;
			}
			if (key.contains("qry_")) {
				String columnValue = strConditions[1];
				String type = strConditions[2];
				String value = ConvertUtil.convertToString(paraMap.get(key));
				if(StringUtil.isEmpty(value) || "[]".equals(value)) {
					continue;
				}
				// 如果是在哪些范围内
				if (SearchConditionType.inType.equals(type)) {
					String[] conditions = value.split(",");
					List<String> list = new ArrayList<>(Arrays.asList(conditions));
					String codes = list.stream().collect(Collectors.joining("','"));
					sql.appendSingle(" and {0} in ({1})", columnValue, "'" + codes + "'");
				} else if (SearchConditionType.likeType.equals(type)) {
					if ("usedcontractcode".equals(columnValue)) {
						// 如果是like
						sql.appendSingle(" and (usedcontractcode like '%{0}%' or contractcode like '%{0}%')", value);
					} else if ("usedpurchasecode".equals(columnValue)) {
						sql.appendSingle(" and (usedpurchasecode like '%{0}%' or purchasecode like '%{0}%')", value);
					} else {
						// 如果是like
						sql.appendSingle(" and {0} like '%{1}%'", columnValue, value);
					}
				} else if (SearchConditionType.equalsType.equals(type)) {
					// 如果是等于
					sql.appendSingle(" and {0} = '{1}'", columnValue, value);
				} else if (SearchConditionType.numberUpType.equals(type)) {
					// 大于等于某个数
					sql.appendSingle(" and {0} >= {1}", columnValue, value);
				} else if (SearchConditionType.numberLowerType.equals(type)) {
					// 小于等于某个数
					sql.appendSingle(" and {0} <= {1}", columnValue, value);
				} else if (SearchConditionType.satrtTimeType.equals(type)) {
					// 大于等于某个日期
					String dataTime = specificTime(strConditions, value);
					sql.appendSingle(" and {0} >=  {1}", columnValue, dataTime);
				} else if (SearchConditionType.endTimeType.equals(type)) {
					// 小于等于某个日期
					if(value.length()==10) {
						value = value + " 23:59:59";
					}
					String dataTime = specificTime(strConditions, value);
					sql.appendSingle(" and {0} <= {1}", columnValue, dataTime);
				}
			}
		}
		sql.appendSingle(" and ");
		return sql.toString();
	}

	/**
	 * 具体时间处理
	 *
	 * @description
	 * @author:HuDeyong
	 * @date:2021年7月2日上午11:08:16
	 * @modify:
	 */
	public String specificTime(String[] strConditions, String value) {
		String dataTime = "";
		// 长度为4，表示有扩展字段，有具体时间 格式要求为add** 如add00 add23 add15等
		if (strConditions.length == 4) {
			String specificTime = strConditions[3].substring(3, strConditions[3].length());
			// 表示0点
			if ("00".equals(specificTime)) {
				dataTime = SQLAdapter.toDateLong(value + " 00:00:00");
			} else {
				dataTime = SQLAdapter.toDateLong(value + specificTime + ":59:59");
			}
		} else {
			dataTime = SQLAdapter.toDateLong(value);
		}
		return dataTime;

	}
}
